<?php

/**
 * user actions.
 *
 * @package    vmdai
 * @subpackage user
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class userActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  }
  
    public function executeCaptcha(sfWebRequest $request)  {
            $img = new securimage();
            $img->image_width = 125;
            $img->image_height = 35;
            $img->gd_font_size = 12;
            $img->perturbation = 0.9;
            $img->code_length = 4;
            $img->image_bg_color = new Securimage_Color("#ffffff");
            $img->use_transparent_text = true;
            $img->text_transparency_percentage = 60; // 100 = completely transparent
            $img->num_lines = 5;
            $img->text_angle_minimum = 0;
            $img->image_signature = '';
            $img->session_name = '';
            $img->text_color = new Securimage_Color("#000000");
            $img->line_color = new Securimage_Color("#cccccc");
            $img->session_space = 'user_reg';
            return $img->show();
    }
     /**
      * Executes index action
      *
      * @param sfRequest $request A request object
      */
      public function executeRegister(sfWebRequest $request){
        $this->getResponse()->setTitle("注册会员--微美贷VMdai官网_中国领先的互联网金融P2P网贷平台_网络贷款_投资理财");
          if( $request->isMethod("POST") ) {
              $code = $request->getPostParameter('validatorCode');
              $current_code = $this->getUser()->getAttribute('securimage_code_value','','user_reg');

              if( strtolower($code) !=  $current_code ){
                  $this->getUser()->setFlash('code_error', '验证码输入错误！请重新输入');
              }else{
                    $this->getUser()->setAttribute('code','','user_reg');
                    $username = trim($request->getParameter("UserName"));
                    $password = trim($request->getParameter("Password"));
                    $mobile = trim($request->getParameter("Mobile"));
                    $auth = trim($request->getParameter("auth"));//手机
                    $reg_from = trim($request->getParameter("reg_from"));
                    
                    $username_length = strlen($username);
                    if(empty($username) || $username_length<6 || $username_length>15){ 
                        $this->getUser()->setFlash('username_error', '请输入6-15个字符的用户名');
                        $this->redirect('user/register');
                    }
                    //用户唯一性
                    $is_only = Doctrine::getTable('User')->isOnly($username);
                    if(!$is_only){
                        $this->getUser()->setFlash('username_error', '该用户名已经存在');
                        $this->redirect('user/register');
                    }
           
                    if(empty($password)){ 
                        $this->getUser()->setFlash('password_error', '请输入密码');
                        $this->redirect('user/register');
                    }
                    
                    $password_length = strlen($password);
                    if($password_length < 8){
                        $this->getUser()->setFlash('password_error', '请输入最小为8个字符的密码');
                        $this->redirect('user/register');
                    }
                    if(!preg_match("/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/",$password)){
                        $this->getUser()->setFlash('password_error', '密码由字母，数字和特殊字符组成，且必须包含字母和数字');
                        $this->redirect('user/register');
                    }
                    
                    $this->checkMobile($mobile);

                    $mobile_result = Doctrine::getTable("UserMobileAuth")->getCode($mobile);
    
                    if(!$mobile_result){
                        $this->getUser()->setFlash('mobile_error', '请输入手机号获取验证码');
                        $this->redirect('user/register');
                    }else{
                        $created_at = $mobile_result->getCreatedAt();
                        if(time() - strtotime($created_at) > 60){
                            $this->getUser()->setFlash('mobile_error', '您的手机验证码已过期，请重新获取');
                            $this->redirect('user/register');
                        }
                    }
                   
                    if($mobile_result->getAuthCode()!=$auth){
                      $this->getUser()->setFlash('mobile_error', '验证码不正确');
                      $this->redirect('user/register');
                    }
                    
                    if($mobile_result->getMobile()!=$mobile){
                      $this->getUser()->setFlash('mobile_error', '手机号码不正确');
                      $this->redirect('user/register');
                    }
                    
                    $user_model = new User();
                    $user_model->attrs = array("phone"=>$mobile);
                    $user_model->setUserName($username);
                    $user_model->setPassword(md5($password));
                    $user_model->setRegFrom($reg_from);
                    $user_model->setIsNew(true);
                    $user_model->save();
                    
                    //@max:添加用户认证信息
                    $user_auth=new UserAuth();
                    $user_auth->setUserId($user_model->getId())
                            ->setPhone(1)
                            ->setPhoneTime(date("Y-m-d H:s:m"))
                            ->save();
                            
                    $this->getUser()->setFlash('success', '注册成功！');
                    $this->redirect('user/login');
              }
          }
      }
      
      protected function checkMobile($mobile){
           if(empty ($mobile)){
                $this->getUser()->setFlash('mobile_error', '请输入手机号码');
                $this->redirect('user/register');
            }

            if(!preg_match("/^(13[0-9]|14[57]|15[0-35-9]|18[0-9]|17[0])[0-9]{8}$/",$mobile)){
                $this->getUser()->setFlash('mobile_error', '请输入正确的手机号码');
                $this->redirect('user/register');
            }
      }
      
      public function executeOriginalmobile(sfWebRequest $request){
          $message = "";
          $result = "";
          
          $mobile= trim($request->getParameter("mobile"));
          $code= trim($request->getParameter("originalmobilecode"));
          $mobile_result = Doctrine::getTable("UserMobileAuth")->getCode($mobile);
                if(!$mobile_result){
                  $this->getUser()->setFlash('mobile_error', '请输入验证码');
                  $this->redirect('user/security?types=3');
                }else{
                  $created_at = $mobile_result->getCreatedAt();
                  if(time() - strtotime($created_at) > 120){
                    $message = '您的验证码已过期，请重新获取';
                    $result = "error";
                    echo json_encode(array("result"=>$result,'message'=>$message));
                    exit;
                  }
                }
                
                if($mobile_result->getAuthCode()!=$code){
                    $message = '验证码不正确';
                    $result = "error";
                    echo json_encode(array("result"=>$result,'message'=>$message));
                    exit;
                    
                }
                
                
                if($mobile_result->getMobile()!=$mobile){
                    $message = '手机号码不正确';
                    $result = "error";
                    echo json_encode(array("result"=>$result,'message'=>$message));
                    exit;
                    
                }
            $type= $request->getParameter("types");  
            if($type=="2"){
              //旧手机号码验证，不做其他操作，新手机号码验证，修改用户验证手机号码
              $user_id = $this->getUser()->getAttribute("user_id");
              $user_attr=  Doctrine::getTable('UserAttr')->findOneByUserId($user_id);
              $user_attr->setAttrValueByName("phone",$mobile);
            }
            $message = '认证成功';
            $result = "ok";
            echo json_encode(array("result"=>$result,'message'=>$message));
            exit;
      }
      
      public function executeOriginalemail(sfWebRequest $request){
          $message = "";
          $result = "";
          
          $email= trim($request->getParameter("email"));
          $code= trim($request->getParameter("originaloldemailcode"));
          $email_result = Doctrine::getTable("UserEmailAuth")->getCode($email);
                if(!$email_result){
                  
                  $message = '请输入验证码';
                  $result = "error";
                  echo json_encode(array("result"=>$result,'message'=>$message));
                  exit;
                }else{
                  $created_at = $email_result->getCreatedAt();
                  if(time() - strtotime($created_at) > 120){
                    $message = '您的验证码已过期，请重新获取';
                    $result = "error";
                    echo json_encode(array("result"=>$result,'message'=>$message));
                    exit;
                    
                  }
                }
                
                if($email_result->getAuthCode()!=$code){
                    $message = '验证码不正确';
                    $result = "error";
                    echo json_encode(array("result"=>$result,'message'=>$message));
                    exit;
                    
                }
                if($email_result->getEmail()!=$email){
                      $message = '邮箱不正确';
                      $result = "error";
                      echo json_encode(array("result"=>$result,'message'=>$message));
                      exit;
                }
               
            $type= $request->getParameter("types");  
            if($type=="2"){
              //旧手机号码验证，不做其他操作，新手机号码验证，修改用户验证手机号码
              $user_id = $this->getUser()->getAttribute("user_id");
              
              $user_attr=  Doctrine::getTable('UserAttr')->findOneByUserId($user_id);
              echo $user_attr->getUserId();exit;
              $user_attr->setAttrValueByName("email",$email);
            }
            $message = '认证成功';
            $result = "ok";
            echo json_encode(array("result"=>$result,'message'=>$message));
            exit;
      }
      
      public function executeCreate_code(sfWebRequest $request){
          $message = "";
          $result = "";
          $mobile = trim($request->getParameter("mobile",0));
          $this->checkMobile($mobile);
          //手机唯一性
          $mobile_only = Doctrine::getTable("UserAttr")->checkMobile($mobile);

          if(!$mobile_only){
              $message = '该手机号已绑定，请不要重复绑定';
              $result = "error";
              echo json_encode(array("result"=>$result,'message'=>$message));
              exit;
          }
          
          //手机验证码
          $mobile_result = Doctrine::getTable("UserMobileAuth")->getCode($mobile);
          if($mobile_result){
              $created_at = $mobile_result->getCreatedAt();
              if(time() - strtotime($created_at) < 60){
                   $message = '一分钟内请勿重复发送验证码！';
                   $result = "error";
                   echo json_encode(array("result"=>$result,'message'=>$message));
                   exit;
              }
          }

          $code = mt_rand(10000, 999999);
          $user_model = new UserMobileAuth();
          $user_model->setMobile($mobile);
          $user_model->setAuthCode($code);
          $user_model->setIp($request->getRemoteAddress());
          $user_model->save();
          
          $this->renderText($code);
          $message = '验证码发送成功！';
          $result = "ok";
          echo json_encode(array("result"=>$result,'message'=>$message));
          exit;
         
      }
      
      public function executeOldphonecode(sfWebRequest $request){
          $message = "";
          $result = "";
          $mobile = trim($request->getParameter("mobile",0));
          $this->checkMobile($mobile);
          
          
          //手机验证码
          $mobile_result = Doctrine::getTable("UserMobileAuth")->getCode($mobile);
          if($mobile_result){
              $created_at = $mobile_result->getCreatedAt();
              if(time() - strtotime($created_at) < 60){
                   $message = '1分钟内请勿重复发送验证码！';
                   $result = "error";
                   echo json_encode(array("result"=>$result,'message'=>$message));
                   exit;
              }
          }

          $code = mt_rand(10000, 999999);
          $user_model = new UserMobileAuth();
          $user_model->setMobile($mobile);
          $user_model->setAuthCode($code);
          $user_model->setIp($request->getRemoteAddress());
          $user_model->save();
          $this->renderText($code);
          $message = '验证码发送成功！';
          $result = "ok";
          echo json_encode(array("result"=>$result,'message'=>$message));
          exit;
      }
      
      
      /**
       * 找回密码，获取验证码
       * @param sfWebRequest $request
       */
      
      public function executeResetpwd_code(sfWebRequest $request){
          
          $message = "";
          $result = "";
          $mobile = trim($request->getParameter("mobile",0));
          $username = trim($request->getParameter("username"));
          
         $user = Doctrine::getTable('User')->getUserNameById($username);
          if(!$user){
             $message = '用户名不存在';
             $result = "error";
             echo json_encode(array("result"=>$result,'message'=>$message));
             exit;
          }
         $user_attr=  Doctrine::getTable('UserAttr')->findOneByUserId($user->getId());
          
          //判断手机号码是否正确
          if($mobile != $user_attr->getAttrValueByName("phone")){
                $message = '手机号码不正确';
                $result = "error";
                echo json_encode(array("result"=>$result,'message'=>$message));
                exit;
          }
          //手机验证码
          $mobile_result = Doctrine::getTable("UserMobileAuth")->getCode($mobile);
          if($mobile_result){
              $created_at = $mobile_result->getCreatedAt();
              if(time() - strtotime($created_at) < 60){
                   $message = '1分钟内请勿重复发送验证码！';
                   $result = "error";
                   echo json_encode(array("result"=>$result,'message'=>$message));
                   exit;
              }
          }

          $code = mt_rand(10000, 999999);
          $user_model = new UserMobileAuth();
          $user_model->setMobile($mobile);
          $user_model->setAuthCode($code);
          $user_model->setIp($request->getRemoteAddress());
          $user_model->save();
          $this->renderText($code);
          $message = '验证码发送成功！';
          $result = "ok";
          echo json_encode(array("result"=>$result,'message'=>$message));
          exit;
      }
      
      
       public function executeEmail_code(sfWebRequest $request){
         //获取邮箱验证码
          $message = "";
          $result = "";
          $email = trim($request->getParameter("email",0));
          
          //手机唯一性
          $email_only = Doctrine::getTable("UserAttr")->checkEmail($email);
          
          if(!$email_only){
              $message = '该邮箱已绑定，请不要重复绑定';
              $result = "error";
              echo json_encode(array("result"=>$result,'message'=>$message));
              exit;
          }
          
          //邮箱验证码
          $email_result = Doctrine::getTable("UserEmailAuth")->getCode($email);
          
          if($email_result){
            
              $created_at = $email_result->getCreatedAt();
              if(time() - strtotime($created_at) < 60){
                   $message = '1分钟内请勿重复发送验证码！';
                   $result = "error";
                   echo json_encode(array("result"=>$result,'message'=>$message));
                   exit;
              }
          }

          $code = mt_rand(10000, 999999);
          $user_model = new UserEmailAuth();
          $user_model->setEmail($email);
          $user_model->setAuthCode($code);
          $user_model->setIp($request->getRemoteAddress());
          $user_model->save();
          $this->renderText($code);
          $message = '验证码发送成功！';
          $result = "ok";
          echo json_encode(array("result"=>$result,'message'=>$message));
          exit;
      }
      
      public function executeOldemailcode(sfWebRequest $request){
        //获取旧邮箱的验证码
          $message = "";
          $result = "";
          $email = trim($request->getParameter("email",0));
          
          
          
          //邮箱验证码
          $email_result = Doctrine::getTable("UserEmailAuth")->getCode($email);
          
          if($email_result){
            
              $created_at = $email_result->getCreatedAt();
              if(time() - strtotime($created_at) < 60){
                   $message = '1分钟内请勿重复发送验证码！';
                   $result = "error";
                   echo json_encode(array("result"=>$result,'message'=>$message));
                   exit;
              }
          }

          $code = mt_rand(10000, 999999);
          $user_model = new UserEmailAuth();
          $user_model->setEmail($email);
          $user_model->setAuthCode($code);
          $user_model->setIp($request->getRemoteAddress());
          $user_model->save();
          $this->renderText($code);
          $message = '验证码发送成功！';
          $result = "ok";
          echo json_encode(array("result"=>$result,'message'=>$message));
          exit;
      }
  
  
   static public $message = array(
        "user_empty"=>"请输入用户名",
        "password_empty"=>"请输入密码",
        "user_password_error"=>"用户名密码错误"
    );
    
    public function executeLogin(sfWebRequest $request){
        $this->getResponse()->setTitle("会员登录--微美贷VMdai官网_中国领先的互联网金融P2P网贷平台_网络贷款_投资理财");
        if($this->getUser()->isAuthenticated()){
            $this->redirect('@homepage');
        }
        
        if( $request->isMethod("POST") ) {
            $username = trim($request->getParameter("username"));
            $password = trim($request->getParameter("password"));
            if(empty ($username)){
                $this->getUser()->setFlash('error', self::$message['user_empty']);
                $this->redirect('user/login');
            }
            if(empty ($password)){
                $this->getUser()->setFlash('error', self::$message['password_empty']);
                $this->redirect('user/login');
            }
            $password = md5($password);
            $admin_info = Doctrine::getTable('User')->isAdmin($username,$password);
            
            if($admin_info){
              $this->getUser()->setAuthenticated(true);
              $this->getUser()->setAttribute('user_id', $admin_info->getId());
              $this->getUser()->setAttribute('user_name', $admin_info->getUserName());
              $admin_info->setUpdatedAt(date("Y-m-d H:i:s",time()))
                         ->save();
              $this->redirect('@homepage');
          }else{
              $this->getUser()->setFlash('error', self::$message['user_password_error']);
          }
        }
        
    }
    
    
    public function executeLogout(sfWebRequest $request){
        $this->getUser()->setAuthenticated(false);
        $this->getUser()->setAttribute('user_id', '');
        $this->getUser()->setAttribute('user_name', '');
        $this->redirect('user/login');
    }
    
    
    public function executeSecurity(sfWebRequest $request){
          $this->getResponse()->setTitle("安全信息--微美贷VMdai官网_中国领先的互联网金融P2P网贷平台_网络贷款_投资理财");
          $user_id = $this->getUser()->getAttribute("user_id");
          
          $this->get_type= $request->getParameter("types");
          if(!$this->get_type){
            $this->get_type=1;
          }
          $this->user_name = $this->getUser()->getAttribute("user_name");
          $this->user = Doctrine::getTable('User')->findOneById($user_id);
          $this->created_at=  $this->user->getCreatedAt();
          
          //判断用户是否验证过身份证
          $user_auth = Doctrine::getTable('UserAuth')->findOneByUserId($user_id);
          $user_attr=  Doctrine::getTable('UserAttr')->findOneByUserId($user_id);
        
          if(empty($user_auth)){
              //没有认证过
              $this->id_card="0";
              $this->email="0";
              $this->phone="0";
              $this->name="";
          }else{
             
            
              if($user_auth->getIdCard() =="0"){
                  $this->id_card=0;
              }else{
                 $this->id_card=$user_attr->getAttrValueByName("id_card");
                 $this->name=$user_attr->getAttrValueByName("name");
                 $this->rep_card=  $this->replaceString($this->id_card,strlen($this->id_card)-1);
                 $this->rep_name=mb_substr($this->name,0,1,'utf-8')."**";
               
              }
              if($user_auth->getEmail() =="0"){
                  $this->email=0;
              }else{
                $this->email=$user_attr->getAttrValueByName("email");
                if($this->email!="" && $this->email !=""){
                  $emailarr=explode("@", $this->email);
                  $this->rep_email= $this->replaceString($emailarr[0],strlen($emailarr[0])-3).$emailarr[1];
                }
                 
                //$this->rep_email= preg_replace($pattern, $replacement, $this->email);
              }
              if($user_auth->getPhone() =="0"){
                  $this->phone=0;
              }else{
                $this->phone=$user_attr->getAttrValueByName("phone");
                
                $pattern = "/(1\d{1,2})\d\d(\d{0,3})/";
                $replacement = "\$1****\$3";

                $this->rep_phone= preg_replace($pattern, $replacement, $this->phone);
              }
          }
          
      
          
          if( $request->isMethod("POST") ) {
             $type= $request->getParameter("type");
             
             if($type =="1"){
               //身份证验证
              $realname= $request->getParameter("realname");
              $idnum= $request->getParameter("idnum");

              $wsdl = "https://api.nciic.com.cn/nciic_ws/services/NciicServices?wsdl";
              $client=new SoapClient($wsdl);

              $inConditions="<?xml version=\"1.0\" encoding=\"UTF-8\" ?><ROWS><INFO><SBM>wmjrwmjr48992</SBM></INFO><ROW><GMSFHM>公民身份号码</GMSFHM><XM>姓名</XM></ROW><ROW FSD=”200080” YWLX=”身份证认证” ><GMSFHM>".$idnum."</GMSFHM><XM>".$realname."</XM></ROW></ROWS>";;
              
              $inLicense='?v?z*_QK/A8rL:&A&(&*3#dsZB?c?o?g?k?h?o?[ZGXM@2SK7)N6cf%S?c?oVkXyHv1;%y?x$`6aWuDfCza3So`V\t>z.lUzMp_y@l\q\lIhJn?x?v\eCsCaM/?x?.b+0z?.;d4.?jUpXjV[Gv6.?va>Nm_wVl?x?..o?h?.<b?v?j[c_eb0[[-;?vCtRyOmQq5sDV?_J]dw.$;&dgILb&0.?m8x-[?v3i\mUo`8W;?x.lPzEb[.JtNt^kPcTj?xd9/p?g4n+y?g?v?jZaWw\hZ/Awa`M[c6?x)qRuGe5y0g?v?jXfBkb6PcMbSo\yXe?x%tNoIdbZOtA.c][pCad9DcYw?x/;EvXhBsW;?x?g1z?g9s?vcOTlTva7Uk?x)q?j]gUjXaLyGgSjOaLnHmFzWr?x?v3iWud=b\WlHmZ/Tk6.';
              //$handle=fopen("../lib/vendor/identity.txt","r");
              //$inLicense = fread($handle, filesize ("../lib/vendor/identity.txt"));

              $parame=array($inLicense,$inConditions);
              $ret = $client->__soapCall("nciicCheck",$parame);
              //$xml = simplexml_load_string($ret); 
              
              print_r($xml); exit;

             }elseif($type =="2"){
               //邮箱认证
              $email= trim($request->getParameter("email"));
              $code= trim($request->getParameter("emailcode"));
              $email_result = Doctrine::getTable("UserEmailAuth")->getCode($email);
                if(!$email_result){
                  $this->getUser()->setFlash('email_error', '请输入邮箱验证码');
                  $this->redirect('user/security?types=2');
                }else{
                  $created_at = $email_result->getCreatedAt();
                  if(time() - strtotime($created_at) > 1200){
                    $this->getUser()->setFlash('email_error', '您的邮箱验证码已过期，请重新获取');
                    $this->redirect('user/security?types=2');
                  }
                }
                
                if($email_result->getAuthCode()!=$code){
                    $this->getUser()->setFlash('email_error', '验证码不正确');
                    $this->redirect('user/security?types=2');
                }
                if($email_result->getEmail()!=$email){
                      $this->getUser()->setFlash('email_error', '邮箱不正确');
                      $this->redirect('user/security?types=2');
                }
                
                
                 $user_attr->setAttrValueByName("email",$email);
                 //添加用户邮箱认证信息
                 $user_auth->setEmail(1)
                           ->setEmailTime(date("Y-m-d H:s:m"))
                           ->save();
                 $this->redirect('user/security');
             }elseif($type =="3"){
               //修改密码
              $pwd= trim($request->getParameter("pwd"));
              $newpwd= trim($request->getParameter("newpwd"));
               if($this->user->getPassword()!=md5($pwd)){
                 $this->getUser()->setFlash('pwd_error', '您的旧密码输入不正确！');
                 $this->redirect('user/security?types=4');
               }
               
                if($this->user->getPassword()==md5($newpwd)){
                 $this->getUser()->setFlash('pwd_error', '新旧密码不能相同！');
                 $this->redirect('user/security?types=4');
               }
               
               //修改密码
               $this->user->setPassword(md5($newpwd))->save();

               $this->redirect('user/security');
             }
            
          }
          
          
    }

   
    public function  replaceString($org,$n)
    {
        $cs =  str_split($org);
        $len = strlen($org);
        
        for ($i = 0; $i < $n; $i++)
        {
            $cs[$len - $i - 1] = '*';
        }
        return implode("", $cs);
    }
    
    public function executeTest(sfWebRequest $request){
	$wsdl = "https://api.nciic.com.cn/nciic_ws/services/NciicServices?wsdl";
	$inConditions="<?xml version=\"1.0\" encoding=\"UTF-8\" ?><ROWS><INFO><SBM>wmjrwmjr48992</SBM></INFO><ROW><GMSFHM>公民身份号码</GMSFHM><XM>姓名</XM></ROW><ROW FSD=”200080” YWLX=”身份证认证” ><GMSFHM>340222098904243827</GMSFHM><XM>梅宇</XM></ROW></ROWS>";;
        $inLicense='?v?z*_QK/A8rL:&A&(&*3#dsZB?c?o?g?k?h?o?[ZGXM@2SK7)N6cf%S?c?oVkXyHv1;%y?x$`6aWuDfCza3So`V\t>z.lUzMp_y@l\q\lIhJn?x?v\eCsCaM/?x?.b+0z?.;d4.?jUpXjV[Gv6.?va>Nm_wVl?x?..o?h?.<b?v?j[c_eb0[[-;?vCtRyOmQq5sDV?_J]dw.$;&dgILb&0.?m8x-[?v3i\mUo`8W;?x.lPzEb[.JtNt^kPcTj?xd9/p?g4n+y?g?v?jZaWw\hZ/Awa`M[c6?x)qRuGe5y0g?v?jXfBkb6PcMbSo\yXe?x%tNoIdbZOtA.c][pCad9DcYw?x/;EvXhBsW;?x?g1z?g9s?vcOTlTva7Uk?x)q?j]gUjXaLyGgSjOaLnHmFzWr?x?v3iWud=b\WlHmZ/Tk6.';
        $client=new SoapClient($wsdl);
//	$parame=array($inLicense,$inConditions);
//        $ret = $client->__soapcall("nciicCheck",$parame);
	var_dump($client);
    }
    
    public function executeBasicinfo(sfWebRequest $request){
      $this->getResponse()->setTitle("基础资料");
      
      $user_id = $this->getUser()->getAttribute("user_id");
     
      $this->user_name = $this->getUser()->getAttribute("user_name");
      
      $user_attr = Doctrine::getTable('UserAttr')->findOneByUserId($user_id);
      
      $this->userattrs=$user_attr->getUserAttrs();
      
      $this->phone= $this->replaceString($this->userattrs['phone'],strlen($this->userattrs['phone'])-3);
      $this->id_card= $this->replaceString($this->userattrs['id_card'],strlen($this->userattrs['id_card'])-3);
      $this->name=mb_substr($this->userattrs['name'],0,1,'utf-8')."**";
      if($this->userattrs['email']!="" && $this->userattrs['email']!="0"){
         $emailarr=explode("@", $this->userattrs['email']);
        $this->email= $this->replaceString($emailarr[0],strlen($emailarr[0])-3).$emailarr[1];
      }
     
      $user = Doctrine::getTable('User')->findOneById($user_id);
      $this->sex=  $user->getSex();
      $this->edu_status=$user_attr::$edu_status;
      $this->has_cart_label=$user_attr::$has_cart_label;
      $this->marray_status=$user_attr::$marray_status;
      $this->residence_status=$user_attr::$residence_status;
      $this->relations_status=$user_attr::$relations_status;
      $companyinfo = Doctrine::getTable('CompanyInfo')->findOneByUserId($user_id);
      if($companyinfo){
        $this->company_info=$companyinfo;
        $this->category_status = $companyinfo::$category_status;
        $this->scale_status = $companyinfo::$scale_status;
        $this->income_status = $companyinfo::$income_status;
      }else{
        $companyinfo=new CompanyInfo();
         $this->company_info=$companyinfo;
        $this->category_status = $companyinfo::$category_status;
        $this->scale_status = $companyinfo::$scale_status;
        $this->income_status = $companyinfo::$income_status;
      }
      
     
    }
    
     public function executeSaveuserinfo(sfWebRequest $request){
     
       if( $request->isMethod("POST") ) {
         $user_id = $this->getUser()->getAttribute("user_id");
         $arr=$request->getPostParameters();
         
         //更新性别
         $sex=$arr["Gender"];
         $user = Doctrine::getTable('User')->findOneById($user_id);
         $user->setSex($sex)->save();
         
         unset($arr["Gender"]);
         $user_attr=  Doctrine::getTable('UserAttr')->findOneByUserId($user_id);
         $user_attr->setUserAttrs($arr);
         
         
         $this->redirect('user/basicinfo');
         
       }
    }
    public function executeSavecompanyinfo(sfWebRequest $request){
     
       if( $request->isMethod("POST") ) {
         $user_id = $this->getUser()->getAttribute("user_id");
         $arr=$request->getPostParameters();
         
         
         $company_info=  Doctrine::getTable('CompanyInfo')->findOneByUserId($user_id);
         if(!$company_info){
           $company_info=new CompanyInfo();
         }
        $company_info->setUserId($user_id)
                  ->setCategory($arr['category'])
                 ->setName($arr['name'])
                 ->setScale($arr['scale'])
                 ->setOfficePhone($arr['office_phone'])
                 ->setJob($arr['job'])
                 ->setAddress($arr['address'])
                 ->setIncome($arr['income'])
                 ->save();
         
         $this->redirect('user/basicinfo');
         
       }
    }
    public function executeSaveotherinfo(sfWebRequest $request){
     
       if( $request->isMethod("POST") ) {
         $user_id = $this->getUser()->getAttribute("user_id");
         $arr=$request->getPostParameters();
         
         
         $user_attr=  Doctrine::getTable('UserAttr')->findOneByUserId($user_id);
         $user_attr->setUserAttrs($arr);
         
         $this->redirect('user/basicinfo');
         
       }
    }
    
    
    /**
     * 找回密码
     */
     public function executeResetpwd(sfWebRequest $request){
        $this->getResponse()->setTitle("找回密码--微美贷VMdai官网_中国领先的互联网金融P2P网贷平台_网络贷款_投资理财");
        $this->message=trim($request->getParameter("message"));
        if( $request->isMethod("POST") ) {
          $user_name=trim($request->getParameter("username"));
          $password=trim($request->getParameter("password"));
          $mobile=trim($request->getParameter("mobile"));
          $auth=trim($request->getParameter("auth"));
          $user = Doctrine::getTable('User')->getUserNameById($user_name);
          if(!$user){
             //$this->sf_user->setFlash('pwd_error', '用户名不存在！');
             $this->redirect('user/resetpwd?message=1');
          }
         $user_attr=  Doctrine::getTable('UserAttr')->findOneByUserId($user->getId());
          
          //判断手机号码是否正确
          if($mobile != $user_attr->getAttrValueByName("phone")){
               //$this->sf_user->setFlash('pwd_error', '手机号码不正确');
               $this->redirect('user/resetpwd?message=2');
          }
          
          
          
          
          $mobile_result = Doctrine::getTable("UserMobileAuth")->getCode($mobile);
                if(!$mobile_result){
                  // $this->sf_user->setFlash('pwd_error', '验证码错误');
                  $this->redirect('user/resetpwd?message=3');
                }else{
                  $created_at = $mobile_result->getCreatedAt();
                  if(time() - strtotime($created_at) > 180){
                    //$this->sf_user->setFlash('pwd_error', '您的验证码已过期，请重新获取');
                   $this->redirect('user/resetpwd?message=4');
                    
                  }
                }
                
                
                
                if($mobile_result->getAuthCode()!=$auth){
                    //$this->sf_user->setFlash('pwd_error', '验证码错误');
                   $this->redirect('user/resetpwd?message=5');
                    
                }
                
                if($mobile_result->getMobile()!=$mobile){
                    // $this->sf_user->setFlash('pwd_error', '手机号码不正确');
                    $this->redirect('user/resetpwd?message=6');
                    
                }
                
                //修改密码
                $user->setPassword(md5($password))->save();
                $this->redirect('user/resetpwd?message=7');
        }
       
    }
}
